package com.test.deencredit.analytics_deencredit.data_sdk_deencredit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.logger_deencredit.ASLger_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcAppsTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcBtyTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcClogTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcCntaTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcHadwTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcSsTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcStorTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASDevcWfiTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.ASFilterOdsTsk_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.OnDevcTskCmple_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit.OnFilterOdsTskCmpt_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.util_deencredit.ASUtil_deencredit;

import java.util.concurrent.CompletableFuture;

public class ASSycManager_deencredit {
    private final static String TAG = "ASSycManager";

    private ASSycManager_deencredit() {
    }

    @SuppressLint("StaticFieldLeak")
    private static ASSycManager_deencredit instance_deencredit;

    public static ASSycManager_deencredit getInstance_deencredit() {
        if (instance_deencredit == null) {
            synchronized (ASSycManager_deencredit.class) {
                instance_deencredit = new ASSycManager_deencredit();
            }
        }
        return instance_deencredit;
    }

    public Context mContext_deencredit;

    private OnDevcTskCmple_deencredit mOnDevcTskCmpledeencredit_deencredit;

    public void init_deencredit(Context applicationContext) {
        mContext_deencredit = applicationContext;
    }

    public void sycData_deencredit(OnDevcTskCmple_deencredit completeListener) {
        try {
            this.mOnDevcTskCmpledeencredit_deencredit = completeListener;

            // 创建任务列表
            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new ASDevcBtyTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new ASDevcStorTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new ASDevcAppsTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new ASDevcWfiTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new ASDevcHadwTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new ASDevcTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(new ASDevcSsTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_MAG));
            CompletableFuture<String> device8Task = CompletableFuture.supplyAsync(new ASDevcCntaTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_COTACT));
            CompletableFuture<String> device9Task = CompletableFuture.supplyAsync(new ASDevcClogTsk_deencredit(mContext_deencredit, ASBuilder_deencredit.T_CAL));

            // 提交任务
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task,
                    device8Task,
                    device9Task);

            // 在所有任务触发后回调，不一定要等结果完成！！！
            allOfFuture.thenRun(() -> {
                // 日记标识
                ASLger_deencredit.d_deencredit(TAG, "sycData: 在所有任务触发后回调");
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (mOnDevcTskCmpledeencredit_deencredit != null) {
                        mOnDevcTskCmpledeencredit_deencredit.onAllCompleted_deencredit();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filterOrders_deencredit(OnFilterOdsTskCmpt_deencredit completeListener) {
        CompletableFuture<String> filterOrdersTask = CompletableFuture.supplyAsync(new ASFilterOdsTsk_deencredit(mContext_deencredit));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(filterOrdersTask);
        allOfFuture.thenRunAsync(() -> {
            String result = ASUtil_deencredit.safeString_deencredit(filterOrdersTask.join()).trim();

            // 日记标识
            ASLger_deencredit.d_deencredit(TAG, "filterOrders: 在所有任务完成后回调");
            ASLger_deencredit.d_deencredit(TAG, "filterOrders result: " + result);

            // 主线程回调
            new Handler(Looper.getMainLooper()).post(() -> {
                if (completeListener != null) {
                    completeListener.onAllCompleted_deencredit(result);
                }
            });
        });
    }
}
